﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Test.AutoUpdater {
    internal static class Utility {
        internal static string GetTempDirectory() {
            var tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            Directory.CreateDirectory(tempDir);
            return tempDir;
        }

        internal static string ConvertToUnit(long byteSize) {
            string str;
            var tempSize = Convert.ToSingle(byteSize);
            if (tempSize / 1024 > 1) {
                if ((tempSize / 1024) / 1024 > 1) {
                    str = ((tempSize / 1024) / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "MB/S";
                } else {
                    str = (tempSize / 1024).ToString("##0.00", CultureInfo.InvariantCulture) + "KB/S";
                }
            } else {
                str = tempSize.ToString(CultureInfo.InvariantCulture) + "B/S";
            }
            return str;
        }

        internal static string GetFileMD5(string fileName) {
            try {
                var file = new FileStream(fileName, FileMode.Open);
                var md5 = new MD5CryptoServiceProvider();
                var retVal = md5.ComputeHash(file);
                file.Close();
                var sb = new StringBuilder();
                for (var i = 0; i < retVal.Length; i++) {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            } catch (Exception) {
                return string.Empty;
            }
        }
    }
}
